package com.ftwj.demo.modular.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.modular.entity.FileBean;
import com.ftwj.demo.modular.service.FileService;
import com.ftwj.demo.modular.vo.entity.FileVo;
import com.ftwj.demo.utils.file.FileUtils;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/file")
@Api(tags="文件管理接口", value="文件管理")
@Validated
public class FileController {

	@Autowired
	private FileService service;
	
	/**
	 * <p>Title: info</p>  
	 * <p>Description: 获取文件详情</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value="获取文件详情", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ResponseBody
	public ResultBean<?> info(@PathVariable String id) {
		FileBean file = service.find(id);
		return ResultBean.success(file);
	}
	
	/**
	 * <p>Title: update</p>  
	 * <p>Description: 修改文件</p>  
	 * @param id
	 * @return 
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@PutMapping("/update/filevo")
	@ApiOperation(value="修改文件", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	@ResponseBody
	public ResultBean<?> update(@RequestBody FileVo fileVo) {
		if(service.updateVo(fileVo) > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: page</p>  
	 * <p>Description: 文件分页列表</p>  
	 * @param current
	 * @param size
	 * @param name
	 * @param modular
	 * @param type
	 * @param state
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@GetMapping("/page")
	@ApiOperation(value="文件分页列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
    	@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
    	,@ApiImplicitParam(name="size", value="每页显示条数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
    })
	@ResponseBody
	public ResultBean<?> page(@RequestParam(required = true) Integer current, @RequestParam(required = true) Integer size
			, @RequestParam(name = "name",required = false) String name
			,@RequestParam(name = "modular",required = false) String modular
			,@RequestParam(name = "type",required = false) Integer type
			,@RequestParam(name = "state",required = false) Integer state) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.page(pageInfo,new FileBean()
								.setName(name)
								.setModular(modular)
								.setType(type)
								.setState(state)
								);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: list</p>  
	 * <p>Description: 文件列表查询</p>  
	 * @param name
	 * @param modular
	 * @param type
	 * @param state
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@GetMapping("/list")
	@ApiOperation(value="文件列表查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ResponseBody
	public ResultBean<?> list(@RequestParam(name = "name",required = false) String name
			,@RequestParam(name = "modular",required = false) String modular
			,@RequestParam(name = "type",required = false) Integer type
			,@RequestParam(name = "state",required = false) Integer state) {
		List<FileBean> list = service.findListByExample(new FileBean()
								.setName(name)
								.setModular(modular)
								.setType(type)
								.setState(state)
								);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: uploadReturnId</p>  
	 * <p>Description: 通用文件上传返回id</p>  
	 * @param multipartFile
	 * @param description
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@PostMapping("/upload/return/id")
	@ResponseBody
	@ApiOperation(value="通用文件上传返回id", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> uploadReturnId(MultipartFile multipartFile,@RequestParam(name="description") String description) {
		String id = service.upload(multipartFile,description,1,"news");
		return ResultBean.success(id);
	}
	
	/**
	 * <p>Title: uploadReturnUrl</p>  
	 * <p>Description: 通用文件上传返回Url</p>  
	 * @param multipartFile
	 * @param description
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@PostMapping("/upload/return/url")
	@ResponseBody
	@ApiOperation(value="通用文件上传返回Url", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> uploadReturnUrl(MultipartFile multipartFile,@RequestParam(name="description") String description) {
		String url = service.upload(multipartFile,description,2,"news");
		return ResultBean.success(url);
	}
	
	/**
	 * <p>Title: download</p>  
	 * <p>Description: 通用文件下载</p>  
	 * @param id
	 * @param request
	 * @param response 
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@GetMapping("/download/{id}")//headers="Accept=application/octet-stream"
	@ApiOperation(value="通用文件下载", notes="数据",code=0,httpMethod=HttpMethod.GET)
	public void download(@PathVariable String id,HttpServletRequest request,HttpServletResponse response) {
		service.downlown(request,response,id);
	}
	
	/**
	 * <p>Title: downloadZip</p>  
	 * <p>Description: 通用文件下载ZIP包</p>  
	 * @param ids
	 * @param request
	 * @param response 
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@GetMapping("/downloadzip/{ids}")
	@ApiOperation(value="通用文件下载ZIP包", notes="数据",code=0,httpMethod=HttpMethod.GET)
	public void downloadZip(@PathVariable String ids,HttpServletRequest request,HttpServletResponse response) {
		service.downloadZip(request,response,ids);
	}
	
	/**
	 * <p>Title: remo</p>  
	 * <p>Description: 逻辑删除文件</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@DeleteMapping("/remo/{id}")
	@ApiOperation(value="逻辑删除文件", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ResponseBody
	public ResultBean<?> remo(@PathVariable String id) {
		if(service.remo(id) > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: delete</p>  
	 * <p>Description: 物理删除文件</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月26日  
	 * @version 1.0
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value="物理删除文件", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ResponseBody
	public ResultBean<?> delete(@PathVariable String id) {
		if(service.delete(id) > 0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: directory</p>  
	 * <p>Description: 获取文件夹下的文件夹</p>  
	 * @param path
	 * @return 
	 * @author HeYang
	 * @date 2020年5月21日  
	 * @version 1.0
	 */
	@GetMapping("/directory")
	@ApiOperation(value="获取文件夹下的文件夹", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ResponseBody
	public ResultBean<List<Map<String, Object>>> directory(@RequestParam(required = true) String path) {
		List<Map<String, Object>> list = FileUtils.getListFileNames(path);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: pageByPath</p>  
	 * <p>Description: 根据文件夹路径获取文件分页列表</p>  
	 * @param current
	 * @param size
	 * @param path
	 * @return 
	 * @author HeYang
	 * @date 2020年5月21日  
	 * @version 1.0
	 */
	@GetMapping("/pageByPath")
	@ApiOperation(value="根据文件夹路径获取文件分页列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
    	@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
    	,@ApiImplicitParam(name="size", value="每页显示条数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
    })
	@ResponseBody
	public ResultBean<?> pageByPath(@RequestParam(required = true) Integer current, @RequestParam(required = true) Integer size
			, @RequestParam(name = "path",required = false) String path) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.page(pageInfo,new FileBean()
								.setPath(path)
								);
		return ResultBean.success(pageInfo);
	}
}
